/*
 This software is the confidential information and copyrighted work of
 NetCracker Technology Corp. ("NetCracker") and/or its suppliers and
 is only distributed under the terms of a separate license agreement
 with NetCracker.
 Use of the software is governed by the terms of the license agreement.
 Any use of this software not in accordance with the license agreement
 is expressly prohibited by law, and may result in severe civil
 and criminal penalties. 
 
 Copyright (c) 1995-2008 NetCracker Technology Corp.
 
 All Rights Reserved.
 
*/
/*
 * Copyright 1995-2007 by NetCracker Technology Corp.,
 * University Office Park III
 * 95 Sawyer Road
 * Waltham, MA 02453
 * United States of America
 * All rights reserved.
 */
package com.flightan.app.airports;

import com.flightan.app.model.Flight;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author: Treshchev 15.07.2011 12:07:20
 */
public class Sheremetyevo
{


    public static void main(String args[]) throws Exception
    {
//        InputStreamReader isr = getConnectionToSite();
//        BufferedReader br = new BufferedReader(isr);
//        HTMLDocument htmlDoc = getHtmlDoc(br);
//
//        ElementIterator iterator = new ElementIterator(htmlDoc);
//        Element element;
//        List<Flight> fligtList = new ArrayList<Flight>();
//        while ((element = iterator.next()) != null)
//        {
//            AttributeSet attributes = element.getAttributes();
//            Object name = attributes.getAttribute(StyleConstants.NameAttribute);
//            if ((name instanceof HTML.Tag) && (name == HTML.Tag.TR))
//            {
//                int count = element.getElementCount();
//                if (count == 10)
//                {
//                    Flight f = new Flight();
//                    for (int i = 0; i < count; i++)
//                    {
//                        f.setCode(getElement(htmlDoc, element, 0));
//                        f.setFlight(getElement(htmlDoc, element, 1));
//                        f.setAirline(getElement(htmlDoc, element, 2));
//                        f.setTo(getElement(htmlDoc, element, 3));
//                        f.setStatus(getElement(htmlDoc, element, 4));
//
//                        f.setDay(getDiffDate("dd MMM", getElement(htmlDoc, element, 5), "dd MMM", "ru"));
//                        f.setDepartureDate(getDiffDate("HH:mm", getElement(htmlDoc, element, 6)));
//                        f.setArrivalDate(getDiffDate("HH:mm", getElement(htmlDoc, element, 7)));
//                        f.setActualDate(getDiffDate("HH:mm", getElement(htmlDoc, element, 8)));
//
//                        f.setTerminal(getElement(htmlDoc, element, 9));
//                    }
//                    fligtList.add(f);
//                }
//
//            }
//        }
//        for (Flight flight : fligtList)
//        {
//            System.out.println("########################");
//            System.out.println(flight.getCode());
//            System.out.println(flight.getFlight());
//            System.out.println(flight.getStatus());
//            System.out.println(flight.getAirline());
//            System.out.println(flight.getTo());
//            System.out.println(flight.getStatus());
//            System.out.println(flight.getDay());
//            System.out.println(flight.getDepartureDate());
//            System.out.println(flight.getArrivalDate());
//            System.out.println(flight.getActualDate());
//            System.out.println(flight.getTerminal());
//        }
//
//
//        System.out.println("Flight count = " + fligtList.size());
    }

    private static Date getDiffDate(String format, String date)
    {
        return getDiffDate(format, date, null, null);
    }

    private static Date getDiffDate(String format, String date, String backFormat, String locale)
    {
        if (date != null)
        {
            SimpleDateFormat sdf;
            if (locale != null)
            {
                sdf = new SimpleDateFormat(format, new Locale("ru", "RU"));
            } else
            {
                sdf = new SimpleDateFormat(format);
            }
            try
            {
                if (backFormat != null)
                {
                    sdf.applyPattern(backFormat);
                }
                return sdf.parse(date);
            }
            catch (ParseException e)
            {
                e.getMessage();
            }
        }
        return null;
    }

//    private static HTMLDocument getHtmlDoc(BufferedReader br) throws IOException
//    {
//        HTMLEditorKit htmlKit = new HTMLEditorKit();
//        HTMLDocument htmlDoc = (HTMLDocument) htmlKit.createDefaultDocument();
//        HTMLEditorKit.Parser parser = new ParserDelegator();
//        HTMLEditorKit.ParserCallback callback = htmlDoc.getReader(0);
//        parser.parse(br, callback, true);
//        return htmlDoc;
//    }

    private static InputStreamReader getConnectionToSite() throws IOException
    {
        URL url = new URL("http://www.svo.aero/timetable/today/");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Accept-Charset", "windows-1251,utf-8;q=0.7,*;q=0.7");

        InputStream is = connection.getInputStream();
        return new InputStreamReader(is, "windows-1251");
    }

//    private static String getElement(HTMLDocument htmlDoc, Element element, int i)
//            throws BadLocationException
//    {
//        Element child = element.getElement(i);
//        AttributeSet childAttributes = child.getAttributes();
//        if (childAttributes.getAttribute(StyleConstants.NameAttribute) == HTML.Tag.TD)
//        {
//            int startOffset = child.getStartOffset();
//            int endOffset = child.getEndOffset();
//            int length = endOffset - startOffset;
//            return htmlDoc.getText(startOffset, length);
//        }
//        return null;
//    }

}
